package com.personalink.management.controller;

import com.personalink.common.dto.ApiResponse;
import com.personalink.common.entity.PersonaConfig;
import com.personalink.management.dto.PersonaSwitchRequest;
import com.personalink.management.dto.PersonaSwitchResponse;
import com.personalink.management.service.PersonaService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * 人格管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/persona")
@RequiredArgsConstructor
@Validated
public class PersonaController {
    
    private final PersonaService personaService;
    
    /**
     * 获取可用人格列表
     */
    @GetMapping("/personas")
    public ApiResponse<List<PersonaConfig>> getPersonas() {
        log.info("获取可用人格列表");
        List<PersonaConfig> personas = personaService.getAllPersonas();
        return ApiResponse.success(personas);
    }
    
    /**
     * 获取指定人格详情
     */
    @GetMapping("/personas/{personaId}")
    public ApiResponse<PersonaConfig> getPersona(@PathVariable String personaId) {
        log.info("获取人格详情: {}", personaId);
        PersonaConfig persona = personaService.getPersonaById(personaId);
        return ApiResponse.success(persona);
    }
    
    /**
     * 切换当前人格
     */
    @PostMapping("/switch")
    public ApiResponse<PersonaSwitchResponse> switchPersona(
            @Valid @RequestBody PersonaSwitchRequest request) {
        log.info("用户 {} 请求切换人格: {} -> {}", 
                request.getUserId(), request.getCurrentPersona(), request.getPersonaId());
        
        PersonaSwitchResponse response = personaService.switchPersona(request);
        return ApiResponse.success("人格切换成功", response);
    }
    
    /**
     * 获取用户当前人格
     */
    @GetMapping("/current/{userId}")
    public ApiResponse<PersonaConfig> getCurrentPersona(@PathVariable String userId) {
        log.info("获取用户 {} 当前人格", userId);
        PersonaConfig persona = personaService.getCurrentPersona(userId);
        return ApiResponse.success(persona);
    }
    
    /**
     * 自定义人格参数
     */
    @PutMapping("/personas/{personaId}/config")
    public ApiResponse<String> updatePersonaConfig(
            @PathVariable String personaId,
            @RequestParam String userId,
            @RequestBody Map<String, Object> config) {
        log.info("用户 {} 更新人格 {} 配置", userId, personaId);
        personaService.updatePersonaConfig(userId, personaId, config);
        return ApiResponse.success("人格配置更新成功");
    }
    
    /**
     * 获取用户人格配置
     */
    @GetMapping("/personas/{personaId}/config")
    public ApiResponse<Map<String, Object>> getPersonaConfig(
            @PathVariable String personaId,
            @RequestParam String userId) {
        log.info("获取用户 {} 的人格 {} 配置", userId, personaId);
        Map<String, Object> config = personaService.getPersonaConfig(userId, personaId);
        return ApiResponse.success(config);
    }
    
    /**
     * 检查人格切换冷却状态
     */
    @GetMapping("/cooldown/{userId}")
    public ApiResponse<Map<String, Object>> getCooldownStatus(@PathVariable String userId) {
        log.info("检查用户 {} 人格切换冷却状态", userId);
        Map<String, Object> status = personaService.getCooldownStatus(userId);
        return ApiResponse.success(status);
    }
    
    /**
     * 重置人格状态（管理员功能）
     */
    @PostMapping("/reset/{userId}")
    public ApiResponse<String> resetPersonaState(@PathVariable String userId) {
        log.info("重置用户 {} 人格状态", userId);
        personaService.resetPersonaState(userId);
        return ApiResponse.success("人格状态重置成功");
    }
}